#!/usr/bin/python
# -- coding:utf8 --
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import model_vn.util_log as utils
from model_vn.utils import select_sql_mysql
from model_vn.utils import select_sql_mysql_v1
import warnings
import pandas as pd 

warnings.filterwarnings('ignore')

def get_age_data(ApplyNO):
    """
    从MySQL获取年龄数据
    :param ApplyNO:
    :return:
    """
    age_sql = f"""
    select distinct 
    UserId
    ,if(Birthday is null, -9999, TIMESTAMPDIFF(YEAR, Birthday, CURDATE())) as age 
    from SysCardAuthentication
    where UserId = (
    select distinct 
    UserId
    from SysJkApply
    where ApplyNO = '{ApplyNO}' 
    )
    """
    age_data = select_sql_mysql_v1(age_sql)
    return age_data

def getEduction(ApplyNO):
    sql=f"""select  ApplyNO,UserId from SysJkApply where ApplyNO='{ApplyNO}'"""
    df_apply= select_sql_mysql_v1(sql)
    dict_main = df_apply.to_dict(orient='records')
    dict_main = dict_main[0]
    MemberId = dict_main['UserId']
    sql2=f"""select  MemberId as UserId,Education from SysBasicInformation where MemberId='{MemberId}'"""
    df_base= select_sql_mysql_v1(sql2)
    df_base.drop_duplicates(subset=['UserId'],inplace=True)
    df_apply=df_apply.merge(df_base,on='UserId',how='left')
    return df_apply

def get_userinfo_freatures(ApplyNO):
    """
    用户基本信息数据的特征集
    :param ApplyNO:
    :return:
    """
    try:
        # user_df = get_age_data(ApplyNO)
        # if user_df.empty:
        #     return {
        #         'age': -9999
        #     }
        # age = user_df.loc[0, 'age']
        
        user_eduction_dataframe = getEduction(ApplyNO)
        if user_eduction_dataframe.empty:
            return {
                'Education': None
            }
        Education = user_eduction_dataframe.loc[0, 'Education']

        return {
            'Education': Education,
        }
    except Exception as e:
        utils.get_logger().error(e)
        return {
            'Education': None,
        }


if __name__ == '__main__':
    ApplyNO = '1679732000463928'
    print(get_userinfo_freatures(ApplyNO))
